package ltd.sshine.websocket.Controller;

import ltd.sshine.websocket.WebSocketStruct.WebSocket;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.UUID;

@Component
@ServerEndpoint(value = "/websocket/{username}")
public class WebSocket2 extends WebSocket {


    @Override
    @OnClose
    public void onClose() {
        super.onClose();
    }

    @Override
    @OnMessage
    public void onMessage(String message) {
        super.onMessage(message);
    }

    //开
        @OnOpen
        public void onOpen(@PathParam(value = "username") String username, Session session){

          super.session=session;
            super.uid= UUID.randomUUID().toString().replaceAll("-","");
            //生成匿名用户
            String rand = RandomStringUtils.randomAlphanumeric(5);
            super.username=username;
            sendGroup(mapUtil.getJsonMap("login","已加入",1,"total",webSocketSet.size()+1,"username",username));
            webSocketSet.put(uid,this);

            sendOne(mapUtil.getJsonMap("login","连接成功",1,"total",webSocketSet.size(),"username",username));
        }


}
